package com.shey.imageloader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.ImageView;

import com.shey.imageloader.bean.BitmapRequest;
import com.shey.imageloader.bean.DoubleCache;
import com.shey.qiuckpictest.R;
import com.shey.utils.BitmapDecoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by SheyChen on 2015/12/2 17:14.
 * Email 1768037936@qq.com
 */
public class MyImageLoader {

    private static final String TAG = "MyImageLoader";
    private static MyImageLoader mMyImageLoader = null;
    private DoubleCache mDoubleCache = null;
    private static Context mContext;

    private MyImageLoader() {
        mDoubleCache = new DoubleCache(mContext);
    }

    public static void init(Context context) {
        if (mContext == null) {
            mContext = context;
        }
    }

    public static MyImageLoader getInstance() {
        if (mMyImageLoader == null) {
            synchronized (MyImageLoader.class) {
                if (mMyImageLoader == null) {
                    mMyImageLoader = new MyImageLoader();
                }
            }
        }
        return mMyImageLoader;
    }

    public void displayImage(ImageView imageView,String path) {
        displayImage(imageView, path ,null,false,true);
    }

    public void displayImage(ImageView imageView, final String path, final LoadeImageCallBack loadeImageCallBack) {
        displayImage(imageView, path, loadeImageCallBack, false,true);
    }

    public void displayImageOriginal(ImageView imageView, String path) {
        displayImage(imageView,path,null,true,true);
    }

    public void displayImageOriginal(ImageView imageView, String path, LoadeImageCallBack loadeImageCallBack) {
        displayImage(imageView,path,loadeImageCallBack,true,true);
    }

    public void displayImageOriginal(ImageView imageView, String path, LoadeImageCallBack loadeImageCallBack,boolean isSetDefault) {
        displayImage(imageView,path,loadeImageCallBack,true,isSetDefault);
    }

    public void displayImage(final ImageView imageView, final String path, final LoadeImageCallBack loadeImageCallBack, final boolean isOriginal, boolean isSetDefaultImage) {
        if (isSetDefaultImage) {
            imageView.setImageDrawable(null);
        }

        if (imageView == null || TextUtils.isEmpty(path)) return;

        BitmapRequest bitmapRequest = new BitmapRequest(imageView, path);

        new LoadImageTask(isOriginal,loadeImageCallBack).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,bitmapRequest);
    }

    public static interface LoadeImageCallBack {
        void onLoadeImage(Bitmap bitmap, ImageView imageView, String uri);
    }

    public DoubleCache getDoubleCache() {
        return mDoubleCache;
    }

    /**
     * 加载显示图片的异步任务
     */
    private class LoadImageTask extends AsyncTask<BitmapRequest,Void,Bitmap> {

        private boolean mIsOriginal = false;
        private LoadeImageCallBack loadeImageCallBack = null;
        private String imageUri = null;
        private ImageView imageView = null;

        public LoadImageTask(boolean isOriginal, LoadeImageCallBack loadeImageCallBack) {
            this.mIsOriginal = isOriginal;
            this.loadeImageCallBack = loadeImageCallBack;
        }

        @Override
        protected Bitmap doInBackground(BitmapRequest... params) {
            final BitmapRequest bitmapRequest = params[0];
            this.imageUri = bitmapRequest.imageUri;
            this.imageView = bitmapRequest.imageViewReference.get();

            Bitmap bitmap = null;

            if (mIsOriginal) {
                //加载原图片
                BitmapDecoder bitmapDecoder = new BitmapDecoder() {
                    @Override
                    public Bitmap decodeBitmapWithOption(BitmapFactory.Options options) {
                        Bitmap bitmap = BitmapFactory.decodeFile(imageUri, options);
                        return bitmap;
                    }
                };
                bitmap = bitmapDecoder.decodeBitmap(0, 0);
            } else {
                //加载图片，按照自己设定的200*200分辨率加载
                bitmap = mDoubleCache.get(bitmapRequest);
            }

            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            if (loadeImageCallBack != null) {
                //回调
                loadeImageCallBack.onLoadeImage(bitmap, imageView, imageUri);
            } else {
                //防止错位
                if (imageView.getTag() != null && imageView.getTag().equals(imageUri)) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }
}